namespace SharpF2.Model
{
	using System;
	using System.Globalization;

	/// <summary>
	/// Define various string case filter
	/// </summary>
	public static class StringFilter
	{
		#region Public Members

		/// <summary>
		/// Lower case all the letters and Upper case only the letter 
		/// following a space caracter.
		/// </summary>
		/// <param name="source">The input string.</param>
		/// <returns>
		/// The case formatted output string.
		/// </returns>
		public static string CapitalizeWords(string source)
		{
			if (String.IsNullOrEmpty(source))
			{
				return string.Empty;
			}

			// convert to char array of the string
			var letters = source.ToCharArray();

			if (letters.Length > 0)
			{
				letters[0] = Char.ToUpper(letters[0], CultureInfo.CurrentCulture);
			}

			for (var i = 0; i < letters.Length - 1; ++i)
			{
				letters[i + 1] = !Char.IsLetterOrDigit(letters[i])
				                 	? Char.ToUpper(letters[i + 1], CultureInfo.CurrentCulture)
				                 	: Char.ToLower(letters[i + 1], CultureInfo.CurrentCulture);
			}

			// return the array made of the new char array
			return new string(letters);
		}

		/// <summary>
		/// Lower case all the letters and Upper case only the first letter. 
		/// </summary>
		/// <param name="input">The input string.</param>
		/// <returns>
		/// The case formatted output string.
		/// </returns>
		public static string FirstLetterUpperCase(string input)
		{
			if (String.IsNullOrEmpty(input))
			{
				return String.Empty;
			}

			input = input.ToLower(CultureInfo.CurrentCulture);

			// Convert to char array of the string
			var letters = input.ToCharArray();
			letters[0] = Char.ToUpper(input[0], CultureInfo.CurrentCulture);

			// Return the array made of the new char array
			return new string(letters);
		}

		#endregion
	}
}